AUTOMAKE_OPTIONS = subdir-objects

.NOTPARALLEL:

SUBDIRS=
DIST_SUBDIRS=
AM_FCFLAGS=-Dnotwin @OPENMP_FCFLAGS@
bin_PROGRAMS= OC
lib_LTLIBRARIES= libOC.la
lib_LTLIBRARIES+= libOPENCALPHAD.la

if WITH_PYTHON
#  SUBDIRS+= OCisoCbinding/pyOC
  PYTHONdir=$(prefix)/python
  dist_PYTHON_DATA = OCisoCbinding/pyOC/pyOC.py \
                     OCisoCbinding/pyOC/pyOCUnitTest.py \
		     OCisoCbinding/pyOC/example.ipynb \
		     OCisoCbinding/pyOC/example.py
  libpyOC_la_SOURCES = OCisoCbinding/pyOC/pyOC.f90
  libpyOC_la_FCFLAGS = -I.
  libpyOC_la_DEPENDENCIES = libOPENCALPHAD.la
  lib_LTLIBRARIES += libpyOC.la
endif

EXTRA_DIST=src/models/gtp3A.F90 \
           src/models/gtp3B.F90 \
           src/models/gtp3C.F90 \
           src/models/gtp3D.F90 \
           src/models/gtp3E.F90 \
           src/models/gtp3F.F90 \
           src/models/gtp3G.F90 \
           src/models/gtp3H.F90 \
           src/models/gtp3X.F90 \
           src/models/gtp3Y.F90 \
           src/models/gtp3Z.F90 \
           src/models/ocparam.F90 \
           src/stepmapplot/smp2A.F90 \
           src/stepmapplot/smp2B.F90 \
           OCisoCbinding/octqc.h \
	   doc/manual/ochelp.html\
	   doc/manual/ochelp.pdf

manualdir=$(pkgdatadir)/doc/manual

dist_manual_SCRIPTS = \
	doc/manual/ochelp.html\
	doc/manual/ochelp.pdf


#############################################
# files used to build the libOC.la library
# It is then used to build the OC executable
# and the libOPENCALPHAD.la shared library
#############################################

libOC_la_SOURCES= \
	src/models/ocparam.F90 \
	src/utilities/metlib4.F90
OC_FCFLAGS=
if WITH_OCHELP
OC_FCFLAGS+= -Dlixed -Dtinyfd -Dlixhlp
endif

if WITH_OCPLOT
OC_FCFLAGS+= -Dx11
endif

if WITH_LAPACK
OC_FCFLAGS +=-Dnotwin
#LAPACKLIBS = -L$(LAPACKLIB) -llapack  -L$(LAPACKLIB) -lrefblas
LAPACKLIBS = -llapack -lblas
else
OC_FCFLAGS +=-Dnotwin -DNOLAPACK
AM_FCFLAGS += -DNOLAPACK
libOC_la_SOURCES +=src/numlib/oclablas.F90
endif

libOC_la_SOURCES +=src/numlib/ocnum.F90 \
	src/numlib/minpack1.F90 \
	src/models/gtp3.F90 \
	src/minimizer/matsmin.F90

########################################
# files used to build the OC executable
########################################
BUILT_SOURCES=libOC.la

OC_SOURCES= \
	src/stepmapplot/smp2.F90 \
	src/userif/pmon6.F90 \
	src/pmain1.F90

OC_LDADD=libOC.la @OPENMPLIB@  $(LAPACKLIBS)
OC_DEPENDENCIES=libOC.la

###########################################################
# files used to build the libOPENCALPHAD.la shared library
###########################################################
libOPENCALPHAD_la_SOURCES= \
	OCisoCbinding/liboctq.F90 \
	OCisoCbinding/liboctqisoc.F90

libOPENCALPHAD_la_LIBADD=libOC.la @OPENMPLIB@
libOPENCALPHAD_la_DEPENDENCIES=libOC.la



############################################################
# Some targets for standalone execution
############################################################

if WITH_PYTHON
PYTHON_MODN = rawpyOC
F90WRAP = ${CURDIR}/f90wrap/bin/f90wrap
F2PY = ${CURDIR}/f90wrap/bin/f2py-f90wrap

all-local: _${PYTHON_MODN}.so

f90wrap:
	git clone https://github.com/jameskermode/f90wrap.git f90wrap
#	cd f90wrap && $(PYTHON) setup.py install*
	touch f90wrap/installedFiles
	cd f90wrap && $(PYTHON) setup.py install --single-version-externally-managed --prefix $(CURDIR)/f90wrap --record $(CURDIR)/f90wrap/installedFiles

_${PYTHON_MODN}.so: export LDFLAGS=-Wl,-rpath=$(libdir) -L./.libs
_${PYTHON_MODN}.so: export NPY_DISTUTILS_APPEND_FLAGS=1
_${PYTHON_MODN}.so: libpyOC.la f90wrap
	$(F90WRAP) -m $(PYTHON_MODN) $(top_srcdir)/OCisoCbinding/pyOC/pyOC.f90 #-v
	$(F2PY) --fcompiler=$(FC) --build-dir . -c -m _$(PYTHON_MODN) -lOPENCALPHAD -L. -lpyOC f90wrap*.f90
	ln -s _${PYTHON_MODN}*.so _${PYTHON_MODN}.so

install-data-hook: export OCPUBLICDATA=$(abs_top_srcdir)/examples/macros
install-data-hook:
	cp _${PYTHON_MODN}.so $(PYTHONdir)
	cp ${PYTHON_MODN}.py $(PYTHONdir)
	cd $(PYTHONdir) && ${PYTHON} pyOCUnitTest.py

endif

.PHONY: doc




clean-local:
	find . -name "*.mod" |xargs rm -f
	find . -name "*.info" |xargs rm -f



install-data-local:
	$(mkinstalldirs) $(DESTDIR)$(bindir)
	@echo "# This script is generated by make install " >$(DESTDIR)$(bindir)/envOC.sh
	@echo "# Use it to set environment for running from install dir" >> $(DESTDIR)$(bindir)/envOC.sh
	@echo ""  >> $(DESTDIR)$(bindir)/envOC.sh
	@echo "## OCHOME " >>$(DESTDIR)$(bindir)/envOC.sh
	@echo "export OCHOME=$(manualdir)/" >>$(DESTDIR)$(bindir)/envOC.sh
	@echo 'LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(DESTDIR)/lib' >> $(DESTDIR)$(bindir)/envOC.sh

	@echo 'PATH=$(DESTDIR)$(bindir):$$PATH' >> $(DESTDIR)$(bindir)/envOC.sh
	@echo "export PATH" >> $(DESTDIR)$(bindir)/envOC.sh
	@echo "" >> $(DESTDIR)$(bindir)/envOC.sh

uninstall-local:
	$(RM) $(DESTDIR)$(bindir)/envOC.sh


distclean-local:
	$(RM) configure.ac configure build_configure.log
	find . -name "Makefile.in" |xargs $(RM) -f
	$(RM) -r config

CLEANFILES=envOC.sh
DISTCLEANFILES=envOC.sh
